using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

namespace GraphEditor.Editor
{
    public class CanvasPopupWindow : EditorWindow
    {
        public VisualTreeAsset popupWindowAsset;


        public static void Show(Rect buttonRect, Vector2 windowSize)
        {
            CanvasPopupWindow window = CreateInstance<CanvasPopupWindow>();
            // 使窗口在失去焦点时自动关闭
            window.Focus();
            window.ShowAsDropDown(buttonRect, windowSize);
        }

        private void CreateGUI()
        {
            // 在这里像正常的 UIToolkit 窗口一样构建UI
            var label = new Label("This is a DropDown Window!");
            label.style.unityTextAlign = TextAnchor.MiddleCenter;
            rootVisualElement.Add(label);

            var button = new Button(() => this.Close()) { text = "Close Me" };
            rootVisualElement.Add(button);
        }

        /// <summary>
        /// 当窗口失去焦点时，自动关闭它
        /// </summary>
        private void OnLostFocus()
        {
            Close();
        }
    }
}